package com.nebula.util;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

public class ExtractKeyUtil {
	private static final String str = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; 
	
	/**
	 * @param n 随机数个数
	 * @return key 提取码
	 */
	public static String getKey(int n){
		String key = "";
		Random random = new Random();
		
		/*int []num = new int[n];
		
		for(int i = 0;i < n;i++){
			int a = random.nextInt(62);
			boolean flag = true;
			for(int j = 0;j < n;j++){
				if(a == num[j]){
					flag = false;
					i--;
					break;
				}
			}
			if(flag){
				num[i] = a;
			}
		}
		for(int i = 0;i < n;i++){
			key += str.substring(num[i],num[i] + 1);
		}*/
		
		for(int i = 0; i < n;i++){
			int a = random.nextInt(62);
			key += str.substring(a, a+1);
		}
		
		return key;
	}
	
	public static void main(String[] args) {
		int n = 10000;
		String []str = new String[n];
		for(int i = 0;i < n;i++){
			str[i] = ExtractKeyUtil.getKey(4);
		}
		int k = 0;
		for(int i = 0;i < n;i++){
			for(int j = i + 1;j < n;j++){
				if(str[i].equals(str[j])){
					System.out.println("提取码重复:" + str[i] + ", " + str[j]);
					k++;
				}
			}
		}
		System.out.println("重复次数: " + k + " 重复概率: " + (k/(double)n)*100 + "%"); 
	}
}
